layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    //用于同步编辑器内容到textarea
    layedit.sync(editIndex);
    var editor = null;
    $(function () {
    	
        var E = window.wangEditor
        editor = new E('#editor');

        //开启debug模式
        editor.customConfig.debug = true;
        // 关闭粘贴内容中的样式
        editor.customConfig.pasteFilterStyle = false
        // 忽略粘贴内容中的图片
        editor.customConfig.pasteIgnoreImg = true

        // 上传图片到服务器
        editor.customConfig.uploadFileName = 'myFile'; //设置文件上传的参数名称
        editor.customConfig.uploadImgServer = 'upload.do'; //设置上传文件的服务器路径
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024; // 将图片大小限制为 3M
        //自定义上传图片事件
        editor.customConfig.uploadImgHooks = {
            before : function(xhr, editor, files) {

            },
            success : function(xhr, editor, result) {
                console.log("上传成功");
            },
            fail : function(xhr, editor, result) {
                console.log("上传失败,原因是"+result);
            },
            error : function(xhr, editor) {
                console.log("上传出错");
            },
            timeout : function(xhr, editor) {
                console.log("上传超时");
            }
        }

        editor.create();


        axios.get("/personal_blog/bloggers/1").then(function(response){
        	var bean = response.data;
        	console.log(bean)
        	$(".userName").val(bean.userName);
        	$(".nickName").val(bean.nickName);
        	$(".sign").val(bean.sign);
        	editor.txt.html(bean.profile);
        	
        	$("#faceImg").attr("src",bean.imageName);
        	
        	form.render();
        })
    })
        

    //上传缩略图
    upload.render({
        elem: '.thumbBox',
        url: '/personal_blog/bloggers/uploadFaceImg',
        field:'faceImg',
        method : "post",  //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
        done: function(res, index, upload){
            $('.thumbImg').attr('src',res.data.src);
            $('.thumbBox').css("background","#fff");
        }
    });

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());
    laydate.render({
        elem: '#release',
        type: 'datetime',
        trigger : "click",
        done : function(value, date, endDate){
            submitTime = value;
        }
    });
    form.on("radio(release)",function(data){
        if(data.elem.title == "定时发布"){
            $(".releaseDate").removeClass("layui-hide");
            $(".releaseDate #release").attr("lay-verify","required");
        }else{
            $(".releaseDate").addClass("layui-hide");
            $(".releaseDate #release").removeAttr("lay-verify");
            submitTime = time.getFullYear()+'-'+(time.getMonth()+1)+'-'+time.getDate()+' '+time.getHours()+':'+time.getMinutes()+':'+time.getSeconds();
        }
    });

    form.verify({
        newsName : function(val){
            if(val == ''){
                return "文章标题不能为空";
            }
        },
        content : function(val){
            if(val == ''){
                return "文章内容不能为空";
            }
        }
    })
    form.on("submit(save)",function(data){
        //截取文章内容中的一部分文字放入文章摘要
       // var abstract = layedit.getText(editIndex).substring(0,50);
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        var bean = {
        		userName:$(".userName").val(),
        		nickName:$(".nickName").val(),
        		sign:$(".sign").val(),
        		profile : editor.txt.html(),
        		imageName:$(".thumbImg").attr("src")
        };

        axios.put("/personal_blog/bloggers",bean).then(function(response){
            setTimeout(function(){
                top.layer.close(index);
                top.layer.msg("个人信息更新成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            },500);

        })
        return false;
    })

    //预览
    form.on("submit(cencel)",function(){
        layer.confirm('确定退出编辑页面吗？', {icon: 3, title: '提示信息'}, function (index) {
        	 top.layer.close(index);
             //刷新父页面
             parent.location.reload();
        })
        
        
        
        
        return false;
    })

    //创建一个编辑器
    var editIndex = layedit.build('news_content',{
        height : 535,
        uploadImage : {
            url : "../../json/newsImg.json"
        }
    });

    function getUrlParms(para){
        var search=location.search; //页面URL的查询部分字符串
        var arrPara=new Array(); //参数数组。数组单项为包含参数名和参数值的字符串，如“para=value”
        var arrVal=new Array(); //参数值数组。用于存储查找到的参数值

        if(search!=""){
            var index=0;
            search=search.substr(1); //去除开头的“?”
            arrPara=search.split("&");

            for(i in arrPara){
                var paraPre=para+"="; //参数前缀。即参数名+“=”，如“para=”
                if(arrPara[i].indexOf(paraPre)==0&& paraPre.length<arrPara[i].length){
                    arrVal[index]=decodeURI(arrPara[i].substr(paraPre.length)); //顺带URI解码避免出现乱码
                    index++;
                }
            }
        }

        if(arrVal.length==1){
            return arrVal[0];
        }else if(arrVal.length==0){
            return null;
        }else{
            return arrVal;
        }
    }




})